/*
 *
 * Copyright 2015 gRPC authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

// Package main implements a client for Greeter service.
package main

import (
	"context"
	"flag"
	"log"
	"time"

	pb "gitee.com/rs1314/taiqiu/proto/dao"
	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials/insecure"
)

var (
	addr           = flag.String("addr", "localhost:60001", "the address to connect to")
	method         = flag.String("method", "get", "update/get user")
	userId         = flag.Int("userId", 1, "userId to set")
	nickName       = flag.String("nickName", "qwenyang", "nickName to set")
	avatarUrl      = flag.String("avatarUrl", "test", "avatarUrl to set")
	noviceTraining = flag.Int("noviceTraining", 0, "userId to set")
	playLevel      = flag.Int("playLevel", 0, "userId to set")
)

func main() {
	flag.Parse()
	// Set up a connection to the server.
	conn, err := grpc.Dial(*addr, grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		log.Fatalf("did not connect: %v", err)
	}
	defer conn.Close()
	c := pb.NewXMetauDaoServiceClient(conn)

	// Contact the server and print out its response.
	ctx, cancel := context.WithTimeout(context.Background(), time.Second)
	defer cancel()
	user := &pb.UserAttribute{
		UserId:         uint64(*userId),
		NickName:       *nickName,
		AvatarUrl:      *avatarUrl,
		NoviceTraining: int32(*noviceTraining),
		PlayLevel:      int32(*playLevel),
	}

	if *method == "get" {
		req := &pb.QueryUserReq{
			UserId: user.UserId,
		}
		r, err := c.QueryUser(ctx, req)
		if err != nil {
			log.Fatalf("query user failed: %v", err)
		}
		log.Printf("Greeting ok %v", r)
	} else if *method == "update" {
		req := &pb.UpdateUserReq{
			User: user,
		}
		r, err := c.UpdateUser(ctx, req)
		if err != nil {
			log.Fatalf("update user failed: %v", err)
		}
		log.Printf("Greeting ok %v", r)
	} else if *method == "userHeader" {
		req := &pb.UpdateUserHeaderReq{
			UserId:    uint64(*userId),
			NickName:  *nickName,
			AvatarUrl: *avatarUrl,
		}
		r, err := c.UpdateUserHeader(ctx, req)
		if err != nil {
			log.Fatalf("update user failed: %v", err)
		}
		log.Printf("Greeting ok %v", r)
	} else if *method == "noviceTraining" {
		req := &pb.UpdateUserTrainingReq{
			UserId:         uint64(*userId),
			NoviceTraining: int32(*noviceTraining),
		}
		r, err := c.UpdateUserNoviceTraining(ctx, req)
		if err != nil {
			log.Fatalf("update user failed: %v", err)
		}
		log.Printf("Greeting ok %v", r)
	} else if *method == "playLevel" {
		req := &pb.UpdateUserLevelReq{
			UserId:    uint64(*userId),
			PlayLevel: int32(*playLevel),
		}
		r, err := c.UpdateUserPlayLevel(ctx, req)
		if err != nil {
			log.Fatalf("update user failed: %v", err)
		}
		log.Printf("Greeting ok %v", r)
	}
}
